package com.chase.passwordmaster.data

import com.chase.passwordmaster.entity.EncodedLoginPassword
import com.chase.passwordmaster.entity.RSAKeyPair
import org.litepal.LitePal

object GlobalData {
    var encryptedLoginPasswordObject: EncodedLoginPassword? = null
    var loginPassword: String? = null
    var rsaKeyPairInString: RSAKeyPair? = null
}

fun initGlobalData() {
    initEncryptedLoginPasswordObject()
    initRSAKeyPairInString()
}

fun refreshGlobalData() {
    initEncryptedLoginPasswordObject()
    initRSAKeyPairInString()
}

private fun initEncryptedLoginPasswordObject() {
    val encryptedLoginPasswordFromDB = LitePal.findAll(EncodedLoginPassword::class.java)
    if (encryptedLoginPasswordFromDB.size > 0) {
        encryptedLoginPasswordFromDB[0]?.let { GlobalData.encryptedLoginPasswordObject = it }
    }
}

private fun initRSAKeyPairInString() {
    val rsaKeyPairInStringFromDB = LitePal.findAll(RSAKeyPair::class.java)
    if (rsaKeyPairInStringFromDB.size > 0) {
        rsaKeyPairInStringFromDB[0]?.let { GlobalData.rsaKeyPairInString = it }
    }
}